package com.yuxi;

/**
 * Created by yuxi on 17/1/27.
 */
public class waitAndSleep {
    public static void main(String[] args) throws InterruptedException {
//        MyThread myThread = new MyThread();
//        new Thread(myThread).start();
//        new Thread(myThread).start();
        Object object = new Object();
        MyThreadOne myThreadOne = new MyThreadOne(object);
        Thread t1 = new Thread(myThreadOne);
        t1.start();
        Thread.sleep(2000);
        object.notify();
//        new Thread(myThreadOne).start();
//        new Thread(myThreadOne).start();
//        object.notify();
    }

    static class MyThread implements Runnable {
        public void run() {
            synchronized (this) {
                try {
                    System.out.println("当前线程休眠并没有释放锁" + Thread.currentThread().getName() + "....." + System.currentTimeMillis());
                    Thread.sleep(5000);
                    System.out.println("当前线程休眠并没有释放锁" + Thread.currentThread().getName() + "....." + System.currentTimeMillis());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class MyThreadOne implements Runnable {
        private Object object;

        public MyThreadOne(Object object) {
            this.object = object;
        }

        public void run() {
            synchronized (object) {
                try {
                    System.out.println("当前线程wait start " + Thread.currentThread().getName() + ".....");
                    object.wait();
                    System.out.println("当前线程等待wait end " + Thread.currentThread().getName() + ".....");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
